import { request } from '../../api/request.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp();


Page({
  data: {
    activeAdvancedDay: 0,
    activeAdvancedName: 1,
    active: 0,//tabs标签页
    activetype: 0,
    // 搜索菜单
    option1: [
      { text: '全部订单', value: 0 },
      { text: '京东到家', value: 1 },
      { text: '拼多多', value: 2 },
      { text: '美团', value: 3 },
      { text: '饿了吗', value: 4 },
    ],
    value1: 0,// 搜索菜单
    newsOrder: [],// 新订单列表
    show: false,//蓝牙设备弹出框
    takeOrder: [],//已接单列表
    advanced: [],// 预定单列表
    advancedDay: {},//预定单时间列表
    delivery: [], //配送单列表
    reminder: [],//已催单列表
    success: [], //交易成功列表
    canc: [], //已取消列表
    after: [], // 售后列表
    // 商家自送
    goodsOrderCloseShow: false, //商家自送弹出框
    username: '',
    userphone: '',
    orderid: '',
    orderType: '',
    // 快购专送
    kgoShow: false,
    deliveryTypes: [],
    r: [],
    checked: true,
    o2oType: '',
    checkOrderid: '',
    kgoCallShow: false,//取消召唤
    callOrderId: '',// 取消召唤订单号
    o2oTypeCall: '',
    typecall: [],//平台召唤中的类型
    menu: 0,//监听搜索选择菜单
    distributionVos: [],// 快购专送列表
    // 催单
    replyReminderList: [
      { value: '商品准备配送,请耐心等待！', index: 1 },
      { value: '送货员已上路,马上就到啦！', index: 2 },
      { value: '天气恶劣，送货员狂奔中！', index: 3 },
      { value: '其它', index: 4 }
    ],
    replyReminderShow: false,
    replyReminderType: '',
    replyReminderOrder: '',
    replyReminderValue: '',
    bg: true,
    // 售后
    turnShow: false,
    inputValue: '',
    turnorderId: '',
    turno2oType: '',


    inputTime: '2020-8-3 14:49:00',
    minute: '',
    second: '',
    time: '',
    t: ''

  },



  onLoad: function (options) {
    request({
      url: '/order/list1',
      method: "POST",
      data: {
        currentPage: 1,
        deliveryTime: 0,
        o2oType: 0,
        orderId: "",
        pageSize: 10,
        status: 1
      },
    }).then(res => {
      console.log(res.data)
      this._formatListData(res.data.o2oOrderVos)
      this._formatstatus(res.data.o2oOrderVos)

      this.setData({
        newsOrder: res.data.o2oOrderVos,
        advancedDay: res.data.deliveryResponses

      })
    })


  },



  // 触发tabs页
  onClick(event) {
    this.setData({
      active: event.detail.name
    })
    if (this.data.active === 1) {
      this.setData({
        activeAdvancedDay: 1
      })
      this.advanced()

    }
    if (this.data.active === 2) {
      this.takeOrder()

    }
    if (this.data.active === 3) {
      this.delivery()
      clearInterval(this.data.time);

    }
    if (this.data.active === 4) {
      this.reminder()

    }
    if (this.data.active === 5) {
      this.success()

    }
    if (this.data.active === 6) {
      this.canc()

    }
    if (this.data.active === 7) {
      this.after()

    }
  },
  // 复制单号
  copyOrder(e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.index,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  // 打印订单
  printOrder() {
    this.setData({
      show: true
    })
    wx.startBluetoothDevicesDiscovery({
      services: ['FEE7'],
      success(res) {
        console.log(res)
      }
    })
    wx.openBluetoothAdapter({
      success(res) {
        console.log(res)
      },
      fail: (res) => {

        console.log("调用异常", res);

      },
      complete(r) {

        console.log("调用结束", r);

      }
    })
    wx.startWifi({
      success(res) {
        console.log(res.errMsg)
      }
    })

  },
  //关闭蓝牙弹出框
  onClose() {
    this.setData({ show: false });
  },
  // 立即接单 
  offOrder(e) {
    const order = e.target.dataset.orderid
    if (e.target.dataset.o2otype === '京东') {
      e.target.dataset.o2otype = 1
    } else if (e.target.dataset.o2otype === "拼多多") {
      e.target.dataset.o2otype = 2
    } else if (e.target.dataset.o2otype === "美团") {
      e.target.dataset.o2otype = 3
    } else {
      e.target.dataset.o2otype = 4
    }
    const type = e.target.dataset.o2otype
    console.log(order)
    request({
      url: '/order/receiving',
      method: "POST",
      data: {
        deliveryTypes: [],
        name: "",
        o2oType: type,
        operationType: 0,
        orderId: order,
        phone: ""
      }
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          activetype: 2
        })
        this.takeOrder()
      }
    })
  },
  // 订单详情
  orderDetail(e) {
    wx.navigateTo({
      url: "./detail/index?orderid=" + e.target.dataset.orderid + '&o2otype=' + e.target.dataset.o2otype + '&o2ostatus=' + e.target.dataset.o2ostatus,
    })

  },
  _formatListData(list) {
    return list.map((item) => {
      if (item.o2oType === 1) {
        item.o2oType = "京东"
      } else if (item.o2oType === 2) {
        item.o2oType = "拼多多"
      } else if (item.o2oType === 3) {
        item.o2oType = "美团"
      } else {
        item.o2oType = "饿了么"
      }
      return item;
    })
  },
  _formatstatus(list) {
    return list.map(x => {
      if (x.status === 1) {
        x.status = "待接单"
      } else if (x.status === 2) {
        x.status = "预定单"
      } else if (x.status === 3) {
        x.status = "已接单"
      } else if (x.status === 4) {
        x.status = "配送中"
      } else if (x.status === 5) {
        x.status = "已催单"
      } else if (x.status === 6) {
        x.status = "售后"
      } else if (x.status === 7) {
        x.status = "交易成功"
      } else if (x.status === 8) {
        x.status = "已取消"
      }
    })
  },
  // 预定单
  advanced() {
    request({
      url: '/order/list1',
      method: "POST",
      data: {
        currentPage: 1,
        deliveryTime: 0,// 0是今天，1是明天，依次往后累加
        o2oType: 0,
        orderId: "",
        pageSize: 10,
        status: 2
      },
    }).then(res => {
      console.log(res.data)
      this._formatListData(res.data.o2oOrderVos)
      this.setData({
        advanced: res.data.o2oOrderVos,
        advancedDay: res.data.deliveryResponses
      })
    })
  },
  // 已接单 
  takeOrder() {
    request({
      url: '/order/list1',
      method: "POST",
      data: {
        currentPage: 1,
        deliveryTime: 0,
        o2oType: 0,
        orderId: "",
        pageSize: 10,
        status: 3
      },
    }).then(res => {
      res.data.o2oOrderVos.map(x => {
        this.setData({
          inputTime: new Date(x.time).getTime()
        })
      })
      console.log(res.data)
      this.setData({
        takeOrder: res.data.o2oOrderVos
      })
    })

  },
  // 配送
  delivery() {
    if (this.data.menu == 0) {
      request({
        url: '/order/list1',
        method: "POST",
        data: {
          currentPage: 1,
          deliveryTime: 0,
          o2oType: 0,
          orderId: "",
          pageSize: 10,
          status: 4
        },
      }).then(res => {
        console.log(res.data)
        // this._formatListData(res.data.o2oOrderVos)
        this.setData({
          delivery: res.data.o2oOrderVos
        })
      })
    } else {
      this.getList()
    }
  },
  // 已催单
  reminder() {
    request({
      url: '/order/list1',
      method: "POST",
      data: {
        currentPage: 1,
        deliveryTime: 0,
        o2oType: 0,
        orderId: "",
        pageSize: 10,
        status: 5
      },
    }).then(res => {
      console.log(res.data)
      this._formatListData(res.data.o2oOrderVos)
      this.setData({
        reminder: res.data.o2oOrderVos
      })
    })
  },
  // 回复催单按钮
  replyReminder(e) {
    if (e.currentTarget.dataset.index.o2oType === '京东') {
      e.currentTarget.dataset.index.o2oType = 1
    } else if (e.currentTarget.dataset.index.o2oType === "拼多多") {
      e.currentTarget.dataset.index.o2oType = 2
    } else if (e.currentTarget.dataset.index.o2oType === "美团") {
      e.currentTarget.dataset.index.o2oType = 3
    } else {
      e.currentTarget.dataset.index.o2oType = 4
    }
    console.log(e.currentTarget.dataset.index)
    this.setData({
      replyReminderShow: true,
      replyReminderType: e.currentTarget.dataset.index.o2oType,
      replyReminderOrder: e.currentTarget.dataset.index.orderId,

    })
  },
  //回复催单信息
  replyReminderChange(e) {
    console.log(e.currentTarget.dataset.index.value)
    this.setData({
      bg: (!this.data.bg)
    })
    this.setData({
      replyReminderShow: true,
      replyReminderType: e.currentTarget.dataset.index.o2oType,
      replyReminderOrder: e.currentTarget.dataset.index.orderId,
      replyReminderValue: e.currentTarget.dataset.index.value
    })
  },
  // 确认回复催单
  replyReminderDialog() {
    request({
      url: '/order/remindReply',
      method: "POST",
      data: {
        o2oType: this.data.replyReminderType,
        orderId: this.data.replyReminderOrder,
        reply: this.data.replyReminderValue
      }
    }).then(res => {
      console.log(res)
    })

  },
  // 交易成功
  success() {
    request({
      url: '/order/list1',
      method: "POST",
      data: {
        currentPage: 1,
        deliveryTime: 0,
        o2oType: 0,
        orderId: "",
        pageSize: 10,
        status: 7
      },
    }).then(res => {
      console.log(res.data)
      this._formatListData(res.data.o2oOrderVos)
      this.setData({
        success: res.data.o2oOrderVos
      })
    })
  },

  // 已取消
  canc() {
    request({
      url: '/order/list1',
      method: "POST",
      data: {
        currentPage: 1,
        deliveryTime: 0,
        o2oType: 0,
        orderId: "",
        pageSize: 10,
        status: 8
      },
    }).then(res => {
      console.log(res.data)
      this._formatListData(res.data.o2oOrderVos)
      this.setData({
        canc: res.data.o2oOrderVos
      })
    })
  },
  // 售后
  after() {
    request({
      url: '/order/list1',
      method: "POST",
      data: {
        currentPage: 1,
        deliveryTime: 0,
        o2oType: 0,
        orderId: "",
        pageSize: 10,
        status: 6
      },
    }).then(res => {
      console.log(res.data)
      this.setData({
        after: res.data.o2oOrderVos,
      })
    })
  },

  // 商家自送
  goodsOrder(e) {
    console.log(e.currentTarget.dataset.item.o2oType)
    this.setData({
      orderType: e.currentTarget.dataset.item.o2oType,
      orderid: e.currentTarget.dataset.item.orderId,
    })

    this.setData({
      goodsOrderCloseShow: true
    })

  },
  // 商家自送确认送达
  goodsOrdersuccess(e) {
    console.log(e.currentTarget.dataset.item.o2oType)
    console.log(e.currentTarget.dataset.item.orderId)
    this.setData({
      successType: e.currentTarget.dataset.item.o2oType,
      successid: e.currentTarget.dataset.item.orderId,
    })
    request({
      url: '/order/deliveryEndOrder',
      method: "POST",
      data: {
        o2oType: e.currentTarget.dataset.item.o2oType,
        orderId: e.currentTarget.dataset.item.orderId
      },
    }).then(res => {
      console.log(res)
    })
  },
  // 监听商家自送关闭
  goodsOrderClose() {
    // this.setData({
    //   goodsOrderCloseShow: false
    // })
  },
  // 监听商家自送关闭
  cancelDialog() {
    this.setData({
      username: '',
      userphone: ''
    })

  },
  // 商家自送名字
  usernameChange(e) {
    this.setData({
      username: e.detail
    })
  },
  // 商家自送手机号
  userphoneChange(e) {
    this.setData({
      userphone: e.detail
    })
  },
  // 确认录入骑手信息(商家自送)
  confirmDialog() {
    const prams = {
      deliveryTypes: [],
      name: this.data.username,
      o2oType: this.data.orderType,
      operationType: 1,
      orderId: this.data.orderid,
      phone: this.data.userphone
    }
    request({
      url: '/order/distribution1',
      method: "POST",
      data: prams,
    }).then(res => {
      console.log(res)
      if (res.code !== 0) {
        return Toast('录入失败')
      }
      Toast('开始商家自送')
      this.setData({
        activetype: 3
      })
      this.delivery()
    })
  },
  // 快购专送
  kgoOrder(e) {
    if (e.currentTarget.dataset.orderid.o2oType === '1') {
      return
    } else {
      this.setData({
        checkOrderid: e.currentTarget.dataset.orderid.orderId,
        o2oType: e.currentTarget.dataset.orderid.o2oType,
        kgoShow: true
      })
      request({
        url: '/order/distributionBilling',
        method: "POST",
        data: {
          orderNo: this.data.checkOrderid,
        }
      }).then(res => {
        this.setData({
          distributionVos: res.data.distributionVos
        })
        res.data.distributionVos.map(x => {
        })
      })
    }
  },
  checkboxChange(event) {
    // console.log(event.currentTarget.dataset.index)
    this.data.r.push(...event.detail.value)
    if (event.detail.value == '') {
      this.data.r.splice(event.currentTarget.dataset.index, 1);
      console.log("数据为空")
    }
    console.log(this.data.r)
  },
  cancelDialogKgo() {
    // this.data.deliveryTypes=[]
  },
  // 确定召唤(快购专送)
  confirmDialogKgo(options) {
    const prams = {
      deliveryTypes: this.data.r,
      name: '',
      o2oType: this.data.o2oType,
      operationType: 2,
      orderId: this.data.checkOrderid,
      phone: ''
    }
    console.log(prams)
    // request({
    //   url: '/order/distribution1',
    //   method: "POST",
    //   data: prams,
    // }).then(res => {

    //   this.setData({
    //     inputTime: new Date(res.data.time).getTime()
    //   })

    //   console.log(res.data)
    //   this.data.distributionVos = res.data
    //   if (res.data.code !== 0) {
    //     return Toast('召唤失败')
    //   }
    // })

    this.countDown1()
    var t = setInterval(this.countDown1, 1000);
    this.setData({
      t: t
    })



    wx.showLoading({
      title: '加载中',
    })
    var that = this
    var time = setInterval(function () {
      that.takeOrder()
      wx.hideLoading()
    }, 3000)
    // 存储定时器,便于清除
    that.setData({
      time: time,
    })
  },
  countDown1() {
    var nowTime = +new Date(); // 返回的是当前时间总的毫秒数
    var times = (this.data.inputTime - nowTime) / 1000; // times是剩余时间总的秒数 
    console.log(times)
    if (times <= 0.68) {
      // 停止定时器
      clearInterval(this.data.t);
      // 停止执行
      return;
    }
    var m = parseInt(times / 60 % 60); // 分
    m = m < 10 ? '0' + m : m;
    this.setData({
      minute: m
    })
    var s = parseInt(times % 60); // 当前的秒
    s = s < 10 ? '0' + s : s;
    this.setData({
      second: s
    })
  },

  // 取消召唤按钮 
  callOrder(e) {
    // console.log(e.currentTarget.dataset.orderid.orderId)
    // console.log(e.currentTarget.dataset.orderid.o2oType)

    if (e.currentTarget.dataset.orderid.o2oType === '1') {
      return
    } else {
      this.setData({
        callOrderId: e.currentTarget.dataset.orderid.orderId,
        o2oTypeCall: e.currentTarget.dataset.orderid.o2oType,
        kgoCallShow: true
      })
      this.data.distributionVos.map(x => {
        this.data.typecall = x.distribution_type
      })
      console.log(this.data.typecall)
    }

  },
  // 确认取消召唤
  confirmDialogKgoCall() {
    const prams = {
      deliveryTypes: ["3"],
      name: '',
      o2oType: this.data.o2oTypeCall,
      operationType: 3,
      orderId: this.data.callOrderId,
      phone: ''
    }
    console.log(prams)
    request({
      url: '/order/distribution1',
      method: "POST",
      data: prams,
    }).then(res => {
      console.log(res)
      if (res.data.code !== 0) {
        return Toast('取消召唤失败')
      }
    })
  },
  // 触发搜索菜单
  changeMenu(e) {
    this.setData({
      menu: e.detail
    })
    this.getList()
  },
  getList() {
    request({
      url: '/order/list1',
      method: "POST",
      data: {
        currentPage: 1,
        deliveryTime: 0,
        o2oType: this.data.menu,
        orderId: "",
        pageSize: 10,
        status: 0
      },
    }).then(res => {
      console.log(res.data)
      this._formatListData(res.data.o2oOrderVos)
      this._formatstatus(res.data.o2oOrderVos)

      this.setData({
        newsOrder: res.data.o2oOrderVos
      })
    })

  },
  // 拒绝退款按钮
  turnOrder(e) {
    console.log(e.currentTarget.dataset.orderid.orderId)
    console.log(e.currentTarget.dataset.orderid.o2oType)
    console.log("拒绝退款")
    this.setData({
      turnShow: true,
      turnorderId: e.currentTarget.dataset.orderid.orderId,
      turno2oType: e.currentTarget.dataset.orderid.o2oType
    })

  },
  // 拒绝退款触发input
  turnChange(e) {
    this.data.inputValue = e.detail.value
    console.log(e.detail.value)
  },
  // 确定拒绝退款
  turnDialog(e) {
    console.log('确定拒绝退款')
    // const r = {
    //   o2oType: this.data.turno2oType,
    //     orderId: this.data.turnorderId,
    //     reason: this.data.inputValue,
    //     type: 2

    // }
    // console.log(r)
    request({
      url: '/order/refund',
      method: "POST",
      data: {
        o2oType: this.data.turno2oType,
        orderId: this.data.turnorderId,
        reason: this.data.inputValue,
        type: 2
      },
    }).then(res => {
      console.log(res)
      if (res.code === 0) {

      }
    })

  },
  // 同意退款
  consentOrder(e) {
    // console.log(e.currentTarget.dataset.orderid.orderId)
    // console.log(e.currentTarget.dataset.orderid.o2oType)
    console.log("同意退款")
    // const r = {
    //   o2oType: e.currentTarget.dataset.orderid.o2oType,
    //   orderId: e.currentTarget.dataset.orderid.orderId,
    //   reason: "",
    //   type: 1

    // }
    // console.log(r)
    request({
      url: '/order/refund',
      method: "POST",
      data: {
        o2oType: e.currentTarget.dataset.orderid.o2oType,
        orderId: e.currentTarget.dataset.orderid.orderId,
        reason: "同意退款",
        type: 1
      },
    }).then(res => {
      console.log(res)
      if (res.code === 0) {
        this.setData({
          activetype: 6
        })

      }
    })
  },
  onClickChiled(e) {
    this.data.activeAdvancedName = e.detail.index
    this.setData({
      activeAdvancedDay: this.data.activeAdvancedName
    })
    console.log(e.detail.index)
    if (e.detail.index == 1) {
      this.advanced()

    }
    else if (e.detail.index == 2) {
      request({
        url: '/order/list1',
        method: "POST",
        data: {
          currentPage: 1,
          deliveryTime: 1,// 0是今天，1是明天，依次往后累加
          o2oType: 0,
          orderId: "",
          pageSize: 10,
          status: 2
        },
      }).then(res => {
        console.log(res.data)
        this._formatListData(res.data.o2oOrderVos)
        this.setData({
          advanced: res.data.o2oOrderVos,
          advancedDay: res.data.deliveryResponses
        })
      })
    } else if (e.detail.index == 3) {
      request({
        url: '/order/list1',
        method: "POST",
        data: {
          currentPage: 1,
          deliveryTime: 2,// 0是今天，1是明天，依次往后累加
          o2oType: 0,
          orderId: "",
          pageSize: 10,
          status: 2
        },
      }).then(res => {
        console.log(res.data)
        this._formatListData(res.data.o2oOrderVos)
        this.setData({
          advanced: res.data.o2oOrderVos,
          advancedDay: res.data.deliveryResponses
        })
      })
    } else if (e.detail.index == 4) {
      request({
        url: '/order/list1',
        method: "POST",
        data: {
          currentPage: 1,
          deliveryTime: 3,// 0是今天，1是明天，依次往后累加
          o2oType: 0,
          orderId: "",
          pageSize: 10,
          status: 2
        },
      }).then(res => {
        console.log(res.data)
        this._formatListData(res.data.o2oOrderVos)
        this.setData({
          advanced: res.data.o2oOrderVos,
        })
      })
    } else {
    }
  },


});

